//
//  AudioPlayViewController.m
//  ocDemo
//
//  Created by Joggy on 16/8/4.
//  Copyright © 2016年 Joggy. All rights reserved.
//

#import "AudioPlayViewController.h"
#import "SKPlayerProgressView.h"
#import "SKAudioPlayer.h"

@interface AudioPlayViewController ()<UITableViewDelegate, UITableViewDataSource> {
    
    UITableView *table;
    UIView *tableViewHeader;
    SKPlayerProgressView *progressView;
    NSArray *data;
    SKAudioPlayer *player;
    NSTimer *timer;
    
}

#define resourcePath(x) [[NSBundle mainBundle] pathForResource:x ofType:@"mp3"]

@end

@implementation AudioPlayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    player = [[SKAudioPlayer alloc] init];
    
    NSArray *dataSource1 = @[@"http://7xlyw8.com1.z0.glb.clouddn.com/JayChou-QianShiQingRen.mp3", @"http://7xlyw8.com1.z0.glb.clouddn.com/MoMingQiMiao-GuMeng.mp3", @"http://7xlyw8.com1.z0.glb.clouddn.com/aKun-ShiSanYue.mp3"];
    NSArray *dataSource2 = @[resourcePath(@"Pianoboy - The truth that you leave"), resourcePath(@"V.K克 - WINGS OF PIANO"), resourcePath(@"Alan Walker - Fade")];
    
    if(_isHTTP) {
        data = dataSource1;
    }
    else {
        data = dataSource2;
    }
    
    table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight) style:UITableViewStylePlain];
    table.delegate = self;
    table.dataSource = self;
    [table registerClass:[UITableViewCell class] forCellReuseIdentifier:@"audioCell"];
    table.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:table];
    
    progressView = [[SKPlayerProgressView alloc] init];
    [self.view addSubview:progressView];
    [progressView.slider addTarget:self action:@selector(audioSeek:) forControlEvents:UIControlEventValueChanged];
    [progressView.pauseButton addTarget:self action:@selector(audioPause:) forControlEvents:UIControlEventTouchUpInside];
    [progressView.stopButton addTarget:self action:@selector(audioStop:) forControlEvents:UIControlEventTouchUpInside];
    [player acceptProgress:progressView.slider];
    [player acceptProgressTimeLabel:progressView.progressLabel];
    [player acceptDurationTimeLabel:progressView.durationLabel];
    
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [player stop];
}

- (void)viewWillLayoutSubviews {
    table.frame = CGRectMake(0, 0, screenWidth, screenHeight);
    tableViewHeader.frame = CGRectMake(0, 0, screenWidth, screenWidth/2);
    progressView.center = CGPointMake(screenWidth/2, screenHeight - progressView.frame.size.height/2 - 64);
}

#pragma mark -- 事件方法
- (void)audioSeek:(UISlider*)slider {
    
}

- (void)audioPause:(UIButton*)button {
    button.selected = !button.selected;
    if(button.selected) {
        [player pause];
    }
    else {
        [player resume];
    }
    NSLog(@"%f", player.progress);
    NSLog(@"%f", player.duration);
}

- (void)audioStop:(UIButton*)button {
    [player stop];
}

#pragma mark -- 代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    if(_isHTTP) {
        [player play:data[indexPath.row] withAllURL:data];
        
    }
    else {
        [player play:data[indexPath.row] withAllPath:data];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return data.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:@"audioCell"];
    NSArray *songTitle = [data[indexPath.row] componentsSeparatedByString:@"/"];
    cell.textLabel.text = songTitle.lastObject;
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
